package org.example.panel.scorepanel;
import javax.swing.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class ModifyScorePanel extends JPanel {
    public ModifyScorePanel(){
        //组件准备
        JTextField num = new JTextField(15);
        JTextField courseName = new JTextField(15);
        JTextField score = new JTextField(15);
        JButton modifyScore = new JButton("修改成绩");
        Box numBox = Box.createHorizontalBox();
        Box courseNameBox = Box.createHorizontalBox();
        Box scoreBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        modifyScore.addActionListener((ActionEvent e)->{
            if(num.getText().isEmpty()||courseName.getText().isEmpty() ||
                    score.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null,
                        "信息不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(num.getText().matches(".*\\s+.*") ||
                    courseName.getText().matches(".*\\s+.*") ||
                    score.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "信息不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(StudentOperation.isNotStudent(num.getText())) {
                JOptionPane.showMessageDialog(null,
                        "学生不存在", null, JOptionPane.WARNING_MESSAGE);
            }else if(!CourseOperation.isCourse(courseName.getText())) {
                JOptionPane.showMessageDialog(null,
                        "课程管理中无此课程", null, JOptionPane.WARNING_MESSAGE);
            }else if(!ScoreOperation.isScore(num.getText(), courseName.getText())) {
                JOptionPane.showMessageDialog(null,
                        "该学生不存在该课程成绩", null, JOptionPane.WARNING_MESSAGE);
            }else{
                try{
                    ScoreOperation.addScore(num.getText(), courseName.getText(),
                            Double.parseDouble(score.getText()));
                    JOptionPane.showMessageDialog(null,
                            "修改成绩成功", null, JOptionPane.WARNING_MESSAGE);
                }catch(Exception e2){
                    JOptionPane.showMessageDialog(null,
                            "成绩格式错误", null, JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        //添加组件
        numBox.add(new JLabel("学号： "));
        numBox.add(num);
        courseNameBox.add(new JLabel("课程名： "));
        courseNameBox.add(courseName);
        scoreBox.add(new JLabel("成绩： "));
        scoreBox.add(score);
        box.add(numBox);
        box.add(Box.createVerticalStrut(5));
        box.add(courseNameBox);
        box.add(Box.createVerticalStrut(5));
        box.add(scoreBox);
        box.add(Box.createVerticalStrut(5));
        box.add(modifyScore);
        add(box);
    }
}
